package com.weapons.week0903_weapons.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.activity.BbsItemActivity;
import com.weapons.week0903_weapons.activity.RecommendContentActivity;
import com.weapons.week0903_weapons.adapter.BbsAdapter;
import com.weapons.week0903_weapons.bean.BbsBean;
import com.weapons.week0903_weapons.constant.UrlString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class BbsFragment extends Fragment {
    private Activity mActivity;
    private List<BbsBean.ItemBean> mDatas;
    private RecyclerView rv_bbs;
    private String url;


    public static BbsFragment newInstance(String url) {
        BbsFragment fragment = new BbsFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDatas = new ArrayList<>();
        mActivity = getActivity();
        if (getArguments() != null) {
            url = getArguments().getString("url");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_bbs, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        rv_bbs = (RecyclerView) view.findViewById(R.id.rv_bbs);
        super.onViewCreated(view, savedInstanceState);
        initData();
        BbsAdapter adapter = new BbsAdapter(mActivity, mDatas);
        adapter.notifyDataSetChanged();
        rv_bbs.setAdapter(adapter);
        LinearLayoutManager linear = new LinearLayoutManager(mActivity);
        rv_bbs.setLayoutManager(linear);
        adapter.setOnItemClickListener(new BbsAdapter.OnRecyclerViewItemClickListener() {
            public void onItemClick(View view, String data) {
                Intent intent = new Intent(getContext(), RecommendContentActivity.class);
                /*intent.setClass(getContext(), BbsItemActivity.class);
                intent.putExtra("data",data);
                Log.i("lala",data+">>>>>>>>>>>>>>>>>");
                //    mContext.startActivity(intent(mContext,BbsItemActivity.class));*/
                intent.putExtra("url", String.format(UrlString.URL_RECOMMEND_CONTENT, data));
                startActivity(intent);
            }
        });

    }

    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                BbsBean bean = new Gson().fromJson(result, BbsBean.class);
                mDatas.addAll(bean.getItem());
            }
        });
    }
}
